package com.mutual.help.controller;

import com.mutual.help.entity.WxUsers;
import com.mutual.help.service.WxUserService;
import com.mutual.help.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 提交测试
 */
@Controller
@RequestMapping("/wx")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    /**
     * 微信端用户点击获取用户信息，先生成id并连同解析的open_id一并保存的数据库中
     * 并返回id，再通过WxRegisterUser()保存用户其他信息
     * @param request
     * @return
     */
    @RequestMapping("/storageInfo/userOpenId")
    @ResponseBody
    public Object userOpenId(HttpServletRequest request){
        String userId = request.getParameter("userId");
        String code = request.getParameter("code");
        return wxUserService.userOpenId(code, userId);
    }

    @RequestMapping("/storageInfo/user")
    @ResponseBody
    public Object WxRegisterUser(HttpServletRequest request){
        String gender = request.getParameter("gender");
        String nickName = request.getParameter("nickName");
        String avatarUrl = request.getParameter("avatarUrl");
        String country = request.getParameter("country");
        return wxUserService.addCustomUser(nickName, gender, avatarUrl, country);
    }

    @RequestMapping("/getWxUserInfoById")
    @ResponseBody
    public Object getWxUserInfoById(HttpServletRequest request){
        String userId = request.getParameter("userId");
        WxUsers wxUser = wxUserService.getById(userId);
        return WeChatResponseUtil.ok("wxUser",wxUser);
    }
}
